<?php
/**
 * @file
 * Manipulator for templates.
 *
 * @category   rpd
 * @package    View
 * @subpackage View
 * @copyright  Copyright (c) 2010 Rpd-Team (http://code.google.com/p/rpd)
 * @license    BSD License
 * @link       http://code.google.com/p/rpd/wiki/Packagename
 * @since      0.0.1
 * @version    0.0.1
 */

/**
 * Uses a view / template and manipulates template variables.
 *
 * Specifiy the used template and set its variables. If nothing is set in
 * controller the called location will be used:
 *
 * @code
 *   // user / visitor calls "example.org/the/site
 *
 *   $view = new View();          // represents "View/the/site.php"
 *   $view = new View("another"); // represents "View/another.php"
 * @endcode
 *
 * Set or change the variables as you wish:
 *
 * @code
 *   $view = new View();
 *   $view->foo = "Hello World";
 *   $view->chg = "today";
 *   $view->chg = "tomorrow";
 * @endcode
 *
 * Your View might look like this:
 *
 * @code
 *   <html>
 *   <body>
 *   <?php echo $this->foo; ?> <br/>
 *   <?php echo $this->chg; ?>
 *   </body>
 *   </html>
 * @endcode
 *
 * If you want to use the data from another instance act like this (e.g. Model):
 *
 * @code
 *   $mdl = new Model_The_Name();
 *   $view = new View();
 *
 *   echo $view($mdl);    // show whole model (repetation of view)
 *   echo $view($mdl[0]); // use only first row in model
 * @endcode
 *
 *
 * @author pretzlaw
 */
class rpd_View implements Rpd_Interface
{
	/**
	 * 
	 * @var array contains used template vars
	 */
	private $_vars = array();
	private $_file = '';
	private $_self = '';
	private $_dir = '';

	public function __get($var)
	{
		if (isset($this->_vars[$var]))
		{
			return $this->_vars[$var];
		}
		return NULL;
	}

	public function __set($var, $val)
	{
		$this->_vars[$var] = $val;
	}

	public function __isset($var)
	{
		return isset($this->_vars[$var]);
	}

	public function __unset($var)
	{
		unset($this->_vars[$var]);
	}

	public function __toString()
	{
		if (file_exists($this->_file))
		{
			ob_start();
			include($this->_file);
			return ob_get_clean();
		}
		return '';
	}

	private function _switchValues($array)
	{
		$this->_vars = array_replace(
		               		$this->_vars,
		               		array_intersect_key($array,$this->_vars)
		);
	}

	public function __invoke($in = NULL)
	{
		if ($in === NULL)
		{
			return $this->_vars;
		}
		else if (is_array($in))
		{
			if (!is_array(current($in)))
			{
				$this->_switchValues($in);
				return $this->__toString();
			}
			else
			{
				$out = '';
				foreach($in as $set)
				{
					$this->_switchValues($set);
					$out .= $this->__toString();
				}
				return $out;
			}
		}
		else if ($in instanceof Model_Abstract)
		{
			$out = '';
			foreach ($in as $mdl)
			{
				foreach ($mdl as $key => $value)
				{
					$this->$key = $value;
				}
				$out .= (string) $this;
			}
			return $out;
		}

		return NULL;
	}

	public function dir($self = true)
	{
		if ($self === true)
		{
			return $this->_self;
		}
		else
		{
			return $this->_dir;
		}
	}

	public function __construct($file = NULL)
	{
		if ($file === NULL)
		{
			$file = 'View/' . $_SERVER['REDIRECT_URL'] . '.php';
		}
		else
		{
			$file = 'View/' . $file . '.php';
		}

		$this->_file = $file;
		$this->_dir = str_replace('\\', '/', dirname($_SERVER['REDIRECT_URL']));
		$this->_self = $_SERVER['REDIRECT_URL'];
	}
}

if (!isset(Hook::$classes["View"]))
{
	class View extends rpd_View {}
}